package com.zz.hhbl.ui

import android.text.Editable
import android.text.InputFilter
import android.text.Spanned
import android.text.TextUtils
import android.text.TextWatcher
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.LinearLayoutManager
import com.zz.framework.core.adapter.KotlinAdapter
import com.zz.framework.core.base.BaseFragment
import com.zz.framework.core.ext.addHorizontalDividerItemDecoration
import com.zz.framework.core.ext.dp
import com.zz.framework.core.ext.finish
import com.zz.framework.core.ext.getInt
import com.zz.framework.core.ext.gone
import com.zz.framework.core.ext.loadUrl
import com.zz.framework.core.ext.onClick
import com.zz.framework.core.ext.toLanguage
import com.zz.framework.core.ext.viewBinding
import com.zz.framework.core.utils.language.Strings
import com.zz.hhbl.R
import com.zz.hhbl.config.GlobalCache
import com.zz.hhbl.databinding.FragmentSearchBinding
import com.zz.hhbl.databinding.ItemPodcastBinding
import com.zz.hhbl.entity.SpotInfoEntity
import com.zz.hhbl.entity.TourGuideCommentaryEntity
import com.zz.hhbl.event.ScanResultEvent
import com.zz.hhbl.event.SearchResultEvent
import com.zz.hhbl.utils.NumberInputTextWatcher
import org.greenrobot.eventbus.EventBus

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/3/10 22:41.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
class SearchFragment : BaseFragment(R.layout.fragment_search) {
    val binding by viewBinding<FragmentSearchBinding>()
    private var mAdapter: KotlinAdapter<TourGuideCommentaryEntity, *>? = null
    override fun initView() {
        super.initView()
        val mSpotInfo: SpotInfoEntity? = GlobalCache.getSpotInfo()
        mSpotInfo?.welcomeData?.firstOrNull()?.let {
            binding?.ivBackground?.loadUrl(it.url.orEmpty())
        }
        binding?.apply {
            btnBack.text = Strings.返回.toLanguage()
            btnBack.onClick {
                finish()
            }
            etContent.setHint(Strings.请输入景点编码.toLanguage())
            tvTitle.setText(Strings.请输入景点编码.toLanguage())
            recyclerView.let {
                it.addHorizontalDividerItemDecoration(15.dp(), android.R.color.transparent, true)
                it.layoutManager = LinearLayoutManager(requireContext())
                mAdapter = KotlinAdapter.Builder<TourGuideCommentaryEntity, ItemPodcastBinding>(ItemPodcastBinding::class.java)
                    .addBindView { holder, item ->
                        run {
                            holder.binding?.apply {
                                tvDetail.text = Strings.详情.toLanguage()
                                ivIcon.loadUrl(item?.exampleImage)
                                tvTitle.text = item?.pointName.orEmpty()
                                ivPlaying.gone()
                                cardView.setCardBackgroundColor(ContextCompat.getColor(requireContext(), R.color.white))
                                tvTitle.setTextColor(ContextCompat.getColor(requireContext(), R.color.color_333333))
                                tvDetail.setTextColor(ContextCompat.getColor(requireContext(), R.color.color_333333))
                                ivMore.setImageResource(R.drawable.vector_more)
                            }
                        }
                    }
                    .onItemClick { holder, item ->
                        item?.run {
                            EventBus.getDefault().post(SearchResultEvent("${item.orderBy}"))
                            finish()
                        }
                    }
                    .create().also { adapter ->
                        recyclerView.adapter = adapter
                    }
            }
            val allList = GlobalCache.getTourGuideCommentary()
            etContent.doAfterTextChanged { text ->
                text?.let {
                    val list = allList.filter { TextUtils.equals("${it.orderBy}", text) }
                    mAdapter?.setNewData(list)
                }
            }
            //过滤输入的数字
            etContent.addTextChangedListener(NumberInputTextWatcher(etContent))
        }
    }
}